package Aptech.Service;

import java.util.Iterator;
import java.util.List;

import Aptech.DAL.MgContract;
import Aptech.DALInterface.ContractInterface;
import HibernateMappingPoJo.PaymentMode;
import HibernateMappingPoJo.PurchaseContract;

public class SV_Contract {
private ContractInterface mgContract;
		public SV_Contract(){
			mgContract = new MgContract();
		}
		
	    public  PurchaseContract[] getAllContract(){
	        List<PurchaseContract>  list = mgContract.getAllContract();
	        String name = " ";
	        PurchaseContract[] listContract = null;
	         if (list != null) {
	        	 listContract = new PurchaseContract[list.size()];
	             Iterator<PurchaseContract> iter = list.iterator();
	             int i = 0;
	             while (iter.hasNext()) {
	            	 PurchaseContract tptd = (PurchaseContract) iter.next();
	            	 name = mgContract.getPaymentModeByID(tptd.getPaymentMode()).getPaymentMode();
	            	 tptd.setPaymentMode(name);
	            	 listContract[i] = tptd;
	                 i++;
	             }
	         }
	         return listContract;
	     }

	    public void addContract(PurchaseContract contract){
	    	mgContract.addContract(contract);
	    }

	    public void deleteContract(String id){
	    	mgContract.deleteContract(id);
	    }
	    
	    
	    public void editContract(PurchaseContract contract){
	    	mgContract.editContract(contract);
	    }
	    
	    public PurchaseContract getContractByID(String id){
	    	return mgContract.getContractByID(id);
	    }
	    
	    public  PaymentMode[] getAllPaymentMode(){
	        List<PaymentMode>  list = mgContract.getAllPaymentMode();

	        PaymentMode[] listModePayment = new PaymentMode[list.size()];
	         if (list != null) {
	             Iterator<PaymentMode> iter = list.iterator();
	             int i = 0;
	             while (iter.hasNext()) {
	            	 PaymentMode tptd = (PaymentMode) iter.next();
	            	 listModePayment[i] = tptd;
	                 i++;
	             }
	         }
	         return listModePayment;
	     }
	    
	    
	    public  PurchaseContract[] searchForName(String key){
	        List<PurchaseContract>  list = mgContract.searchForName(key);
	        String name = " ";
	        PurchaseContract[] listContract = null;
	         if (list != null) {
	        	 listContract = new PurchaseContract[list.size()];
	             Iterator<PurchaseContract> iter = list.iterator();
	             int i = 0;
	             while (iter.hasNext()) {
	            	 PurchaseContract tptd = (PurchaseContract) iter.next();
	            	 name = mgContract.getPaymentModeByID(tptd.getPaymentMode()).getPaymentMode();
	            	 tptd.setPaymentMode(name);
	            	 listContract[i] = tptd;
	                 i++;
	             }
	         }
	         return listContract;
	     }
}
